<?php
require_once "./db_config.php";
if(isset($_POST['status'])){
    $status=$_POST['status'];
//    保存attrbuteJSON
    if($status=='0'){
        $id=$_POST['id'];
        $belongTo=$_POST['belongTo'];
        $attributeJSON=$_POST['attributeJSON'];
        $sql="INSERT INTO dynamic_form_list(id,belongTo,attributeJSON) value('$id','$belongTo','$attributeJSON')";
        $result=mysqli_query($conn,$sql);
        if(!$result){
            die('error: ' . mysqli_error($conn));
        }else{
            echo "success";
        }
//        修改/新增动态表单
    }else if($status=='1'){
        if(isset($_POST['name'])){
            $name=$_POST['name'];
        }
        if(isset($_POST['oldName'])){
            $oldName=$_POST['oldName'];
        }
        if(isset($_POST['type'])){
            $type=$_POST['type'];
        }
        if(isset($_POST['enable'])){
            $enable=$_POST['enable'];
        }
        $update=$_POST['update'];
        $creater=$_POST['creater'];
        $desc=$_POST['desc'];
        if(isset($_POST['isNew'])){
            $sql="INSERT INTO dynamic_form (`name`,`type`,`enable`,`update`,`create`,`creater`,`desc`) VALUE ('$name','$type','$enable','$update','$update','$creater','$desc')";
        }else{
            $sql="UPDATE dynamic_form SET `name`='$name',`type`='$type',`enable`='$enable',`update`='$update',`creater`='$creater',`desc`='$desc' WHERE `name`='$oldName'";
        }
        $result=mysqli_query($conn,$sql);
        if(!$result){
            die('error: ' . mysqli_error($conn));
        }else{
            echo "success";
        }
//        修改动态表单属性
    }else if($status=='2'){
        if(isset($_POST['name'])){
            $name=$_POST['name'];
        }
        if(isset($_POST['oldName'])){
            $oldName=$_POST['oldName'];
        }
        if(isset($_POST['belongTo'])){
            $belongTo=$_POST['belongTo'];
        }
        if(isset($_POST['enable'])){
            $enable=$_POST['enable'];
        }
        if(isset($_POST['update'])){
            $update=$_POST['update'];
        }
        if(isset($_POST['creater'])){
            $creater=$_POST['creater'];
        }
        if(isset($_POST['desc'])){
          $desc=$_POST['desc'];
        }
        if(isset($_POST['isNew'])) {
            $sql = "INSERT INTO dynamic_form_properties (`name`,`belongTo`,`enable`,`update`,`creater`,`desc`) VALUE ('$name','$belongTo','$enable','$update','$creater','$desc')";
            $msg = "属性添加成功！";
        }
        else if(isset($_POST['isDelete'])){
            $sql = "UPDATE dynamic_form_properties SET `delete`='1' WHERE id='$name'";
            $msg = "属性删除成功！";
        }else{
            $sql="UPDATE dynamic_form_properties SET `name`='$name',`belongTo`='$belongTo',`enable`='$enable',`update`='$update',`creater`='$creater',`desc`='$desc' WHERE `name`='$oldName'";
            $msg="属性修改成功！";
        }
        $result=mysqli_query($conn,$sql);
        if(!$result){
            die('error: ' . mysqli_error($conn));
        }else{
            echo $msg;
        }
    }
}
mysqli_close($conn);
